package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.exception.BaseException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private StatementServiceImpl statementService;
    @Autowired
    private IAppointmentService appointmentService;

    @Override
    public List query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        return statementItemMapper.query(qo);
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
    //进行合理性校验
        if (items == null || items.size() <= 0) {
            throw new BaseException("非法操作");
        }
        //删除最后一条数据
        StatementItem tempItem = items.remove(items.size() - 1);
        Long statementId = tempItem.getStatementId();
        BigDecimal disCountPrice = tempItem.getItemPrice();
        Statement statement = statementService.get(statementId);
        //判断是否支付状态
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BaseException("已支付的结算单不能进行保存操作");
        }
        //删除之前的明细的数据  中间表数据
        statementItemMapper.deleteByStatementId(statementId);

        BigDecimal totalAmount = new BigDecimal(0);
        BigDecimal totalQuantity = new BigDecimal(0);
        for (StatementItem item : items) {
            //保存到数据库中
            statementItemMapper.insert(item);
            //总金额     multiply 算法乘 *
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            //总数量
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        //对优惠价格做判断
        if (disCountPrice.compareTo(totalAmount) > 0) {
            throw new BaseException("`非法操作");
        }
        //更新结算单的总消费金额,总数量,总折扣金额
        statementService.updateAmount(statementId,totalAmount,totalQuantity,disCountPrice);
    }

    @Override
    public void pay(Long statementId) {
        //取出当前用户
        Long userId = ShiroUtils.getUserId();
        //修改结算单状态
        //更新状态
        statementService.pay(statementId,userId,Statement.STATUS_PAID);
        //判断当前结算单是否有对应预约单,如果有则修改状态
        Statement statement = statementService.get(statementId);
        if (statement.getAppointmentId()!=null) {
            appointmentService.changeStatus(statement.getAppointmentId(), Appointment.STATUS_PAID);
        }
    }

    @Override
    public void deleteByStatementId(Long id) {
        statementItemMapper.deleteByStatementId(id);
    }
}

